REALbasic version 2 introduces the concept of binding. Binding allows you to add functionality to your interface without writing any code. There are many binding actions you can choose that are built-in to REALbasic but you can also create your own. One of the neat things about creating your own binding actions is that once created, the user doesn't actually have to look at or understand the code that handles the action. You can drop your custom binding into any project and it will work as transparently just as if it was built-in to REALbasic.
Explanation
This first example creates a user defined bind between pushbuttons and listboxes. It allows the user to create a pushbutton that deletes all rows from a listbox.
1. Add a new class to your project.
2. Name this class "DeleteAllRowsBind".
3. Since this class is going to be a bind, it needs to support the bindingInterface. Also, since you will want to assign an action to the Action event of the pushbutton that when clicked, tells the listbox to delete all the rows. In order to add code that will execute when the user clicks the pushbutton, you will need to support the ActionNotificationReceiver interface. To support the bindingInterface and ActionNotificationReciever interface, enter "bindingInterface,ActionNotificationReceiver" in the Interfaces property of the class.
4. Now that the class supports the bindingInterface, you can add the Bind method. Add a new method to the class and called it "Bind". Include the parameters "Source as Object, Target as Object".
5. Since the code that executes when the user presses the pushbutton will need to know which pushbutton and listbox have been bound, these will need to be stored as properties in the class. Add the following properties to the class: BindSource as Pushbutton button and BindTarget as Listbox.
6. Add the following code to the Bind method:
#pragma bindingSpecification pushbutton,listbox,"Delete all rows in %2 when %1 is pushed"
BindSource=pushbutton(source)
bindTarget=listbox(target)
BindSource.addActionNotificationReceiver self
The #pragma statement defines what will appear in the Bind dialog box when the user binds the two objects by command-shift dragging from the source to the target (the pushbutton to the listbox). It indicates the source class, the target class and the text that will appear in the Bind window. %1 will be the name of the source control and %2 will be the name of the target control.
When the bind occurs, the controls that are bound will be passed to the Bind method. However, they are passed as generic objects. In order for the code to store them in the BindSource and BindTarget properties you just added to the class, these two object parameters have to be recast as pushbutton and listbox. Lines 2 and 3 in the Bind method do just that.
finally, the last line connects the bind class to the Action event of the pushbutton by calling the addActionNotificationReceiver method of the pushbutton class. Self is passed as a parameter to this method and represents the class itself.
7. Now you need to add the code that will delete the rows when the user clicks the pushbutton. To do this, add a method called "PerformAction" to the class.
8. The code for the PerformAction method is simple:
bindTarget.DeleteAllRows
9. To try out this new bind, drag a listbox and a pushbutton onto a window, add some rows of data to the listbox then link the pushbutton to the listbox by command-shift dragging from the pushbutton to the listbox. When the Bind dialog appears, choose the option "Delete all rows from listbox1 when pushbutton1 is pushed" then run the project and click the button.
Creating a Bind that deletes only the selected row
1. To create a bind that will delete the selected row from a listbox when a pushbutton is pushed, create another class in your project and duplicate all the steps you did above.
2. Since this version of the bind should only work when a row is selected, you will need to support a few more interfaces in order to detect when the user selects a row. Add listSelectionNotificationReceiver to the list of interfaces in the Interfaces property of the class.
3. Change the last parameter of the #pragma statement in the Bind method to "Delete the selected row in %2 when %1 is pushed".
4. Add the the following two lines to the end of the Bind method:
5. The first line links the bind class to any change to the list selection. The second line, sets the source (the pushbutton) to enabled if a row of the target (the listbox) is selected and disables the source if no row is selected when the window opens.
6. Now you need to add the code that will disable or enable the pushbutton once the window is open the user begins selecting or deselecting rows in the listbox. In order to do this, you will need to add the methods that are part of the ListSelectionNotificationReceiver interface to the class. Add methods "SelectionChanged" and "SelectionChanging" to the class. Niether has any parameters. Add the following code to the SelectionChanged method:
bindSource.enabled=(bindTarget.listindex >= 0)
Anytime the selection in the listbox is changed, the bind will be notified and the SelectionChanged method will fire.
7. Lastly, you need to add the code that will delete the selected row when the user clicks the pushbutton. Add the following code to the PerformAction method:
bindTarget.removeRow bindTarget.listindex
8. Now you can add another button and bind it to the listbox choosing your new bind as the bind action.